home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cserial / screen.c < prev    next >
C/C++ Source or Header  |  1990-04-04  |  10KB  |  434 lines

  1. /*
  2.  *                              SCREEN.C
  3.  *
  4.  *                         Screen i/o Handler
  5.  *
  6.  *                           Written for the
  7.  *
  8.  *                              Datalight
  9.  *                           Microsoft V 5.x
  10.  *                                TurboC
  11.  *                                  &
  12.  *                               Zortech
  13.  *
  14.  *                             C Compilers
  15.  *
  16.  *            Copyright (c) John Birchfield 1987, 1988, 1989
  17.  */
  18.  
  19. #include <dos.h>
  20. #include "screen.h"
  21. /*
  22.  *    defines for the bits returned in regs.x.cflag and by int86 () ...
  23.  */
  24.  
  25. #define ZERO_BIT       0x040
  26. #define CARRY_BIT          1
  27. #define SIGN_BIT       0x080
  28. #define DIRECTION_FLAG 0x400
  29. #define OVERFLOW_BIT   0x800
  30. static union REGS regs;
  31.  
  32.  
  33.  
  34. #define VIDEO        0x10    /* interrupt for dealing with screen */
  35. #define MODE         0      /* code for setting new screen mode */
  36. #define SETCURTYP     1      /* code for setting new cursor type */
  37. #define SETCURSOR     2      /* code for addressing cursor */
  38. #define GETCURSOR     3      /* code for reading cursor location */
  39. #define READLP         4      /* code for reading light pen position */
  40. #define SETPAGE         5      /* code to select active page */
  41. #define SCROLLUP     6      /* code to scroll screen up */
  42. #define SCROLLDN     7      /* code to scroll screen nown */
  43. #define READCH         8      /* code to read a character from screen */
  44. #define WRITEACH     9      /* code to write char and attributes */
  45. #define WRITECH        10      /* code to write character only */
  46. #define SETPAL        11      /* code to set new setpal or border */
  47. #define WDOT        12      /* code to write a dot */
  48. #define RDOT        13      /* code to read a dot */
  49. #define WRITETTY    14      /* code to write as if teletype */
  50. #define STATE        15      /* code to find current screen status */
  51.  
  52.  
  53. /*
  54.  *    note- make 25 for ms-dos and 24 for cp/m as cp/m steals the bottom
  55.  *    line.
  56.  */
  57.  
  58. int     Scr_Rows = 25;  /* current number of rows */
  59. int     Scr_Cols = 80;  /* current number of columns */
  60. char    Scr_Mode = 0;   /* current screen mode */
  61. char    Scr_Page = 0;   /* current page */
  62. char    Scr_ATTR = 7;   /* current attributes for screen 7 is white letters
  63.                          * on black    */
  64. char    Scr_Window_Top = 0;     /* first line to scroll */
  65.  
  66.  
  67.  
  68. /*
  69.  *    SCREEN_INIT - screen_init must be called before any use of any
  70.  *               other routine unless the starting mode is 80X25
  71.  *             character mode (3,4 or 7). Must be called for
  72.  *               monocrome (mode 7) for scr_curson to set a proper
  73.  *               cursor.
  74.  *
  75.  *    Usage:      screen_init ();
  76.  */
  77.  
  78. int 
  79. screen_init (void)
  80. {
  81.     regs.h.ah = STATE;
  82.     int86 (VIDEO, ®s, ®s);
  83.     Scr_Mode = regs.h.al;
  84.     Scr_Cols = regs.h.ah;
  85.     Scr_Page = regs.h.bh;
  86.     Scr_ATTR = (regs.h.al < 4 || regs.h.al == 7) ? 7 : 0;
  87.     return (regs.h.al);
  88. }
  89.  
  90.  
  91.  
  92.  
  93. /*
  94.  *    SCREEN_SMODE - set a new screen mode
  95.  *
  96.  *    Usage:        screen_smode (new mode);
  97.  */
  98.  
  99. int 
  100. screen_smode (char newmode)
  101. {
  102.     regs.h.al = newmode;
  103.     regs.h.ah = MODE;
  104.     int86 (VIDEO, ®s, ®s);
  105.     screen_init ();
  106. }
  107.  
  108.  
  109.  
  110. /*
  111.  *    ROWCOL - sets cursor at any location.
  112.  *
  113.  *    Usage:       rowcol (new row, new column);
  114.  */
  115.  
  116. int 
  117. rowcol (int row, int col)
  118. {
  119.     regs.h.dl = col;
  120.     regs.h.dh = row;
  121.     regs.h.bh = Scr_Page;
  122.     regs.h.ah = SETCURSOR;
  123.     int86 (VIDEO, ®s, ®s);
  124. }
  125.  
  126.  
  127.  
  128. /*
  129.  *  G_ROWCOL - returns the current screen row and column
  130.  *               the row is the high order 8 bits and the
  131.  *               column is the low order.
  132.  *
  133.  *    Usage:     rowcol = g_rowcol ();
  134.  *               cur_row = rowcol >> 8;
  135.  *               cur_col = rowcol && 0xFF;
  136.  */
  137.  
  138. int 
  139. g_rowcol (void)
  140. {
  141.     regs.h.ah = GETCURSOR;
  142.     regs.h.bh = Scr_Page;
  143.     int86 (VIDEO, ®s, ®s);
  144.     return (regs.x.dx);
  145. }
  146.  
  147.  
  148.  
  149.  
  150. /*
  151.  *    CLRSCRN - clear entire screen
  152.  *
  153.  *    Usage:    clrscrn ();
  154.  */
  155.  
  156. int 
  157. clrscrn (void)
  158. {
  159.     regs.h.al = 0;
  160.     regs.x.cx = 0;
  161.     regs.h.dh = Scr_Rows - 1;
  162.     regs.h.dl = Scr_Cols - 1;
  163.     regs.h.bh = Scr_ATTR;
  164.     regs.h.ah = SCROLLUP;
  165.     int86 (VIDEO, ®s, ®s);
  166. }
  167.  
  168.  
  169.  
  170.  
  171. /*
  172.  *    CLEOL - clear to End Of Line
  173.  *
  174.  *    Usage:     cleol ();
  175.  */
  176.  
  177. int 
  178. cleol (void)
  179. {
  180.     regs.h.bh = Scr_Page;
  181.     regs.h.ah = GETCURSOR;
  182.     int86 (VIDEO, ®s, ®s);
  183.     regs.h.cl = Scr_Cols - regs.h.dl;
  184.     regs.h.ch = 0;
  185.     regs.h.al = ' ';
  186.     regs.h.bl = Scr_ATTR;
  187.     regs.h.bh = Scr_Page;
  188.     regs.h.ah = WRITEACH;
  189.     int86 (VIDEO, ®s, ®s);
  190. }
  191.  
  192.  
  193.  
  194.  
  195. /*
  196.  *    CLEOP - clear to End Of Page
  197.  *
  198.  *    Usage:    cleop ();
  199.  */
  200.  
  201. int 
  202. cleop (void)
  203. {
  204.     cleol ();
  205.     regs.h.ah = GETCURSOR;
  206.     regs.h.bh = Scr_Page;
  207.     int86 (VIDEO, ®s, ®s);
  208.     regs.h.al = 0;
  209.     if ((regs.h.ch = regs.h.dh + 1) < Scr_Rows - 1)
  210.     {
  211.         regs.h.cl = 0;
  212.         regs.h.dh = Scr_Rows - 1;
  213.         regs.h.dl = Scr_Cols - 1;
  214.         regs.h.bh = Scr_ATTR;
  215.         regs.h.ah = SCROLLUP;
  216.         int86 (VIDEO, ®s, ®s);
  217.     }
  218. }
  219.  
  220.  
  221.  
  222. /*
  223.  *    SCROLL_UP - Scroll the screen up. The window is scrolled
  224.  *                up nlines lines. A zero nlines will clear the
  225.  *                window. Top left of the screen is 0,0.
  226.  *
  227.  *    Usage:      SCROLL_UP (nlines, start_row, start_col, end_row, end_col);
  228.  */
  229.  
  230. int 
  231. scroll_up (int nlines, 
  232.            int start_row, int start_col,
  233.            int end_row,   int end_col)
  234. {
  235.     regs.h.al = nlines;
  236.     regs.h.ch = start_row;
  237.     regs.h.cl = start_col;
  238.     regs.h.dh = end_row;
  239.     regs.h.dl = end_col;
  240.     regs.h.bh = Scr_ATTR;
  241.     regs.h.ah = SCROLLUP;
  242.     int86 (VIDEO, ®s, ®s);
  243. }
  244.  
  245.  
  246.  
  247. /*
  248.  *    SCROLL_DN - scroll the screen down. the window is scrolled
  249.  *                down nline lines. A zero nline will clear the
  250.  *                window. Top left of the screen in 0,0.
  251.  *
  252.  *    Usage:      scroll_dn (nlines,start_row, start_col, end_row, end_col);
  253.  */
  254.  
  255. int 
  256. scroll_dn (int nlines,
  257.            int start_row, int start_col,
  258.            int end_row,   int end_col)
  259. {
  260.     regs.h.al = nlines;
  261.     regs.h.ch = start_row;
  262.     regs.h.cl = start_col;
  263.     regs.h.dh = end_row;
  264.     regs.h.dl = end_col;
  265.     regs.h.bh = Scr_ATTR;
  266.     regs.h.ah = SCROLLDN;
  267.     int86 (VIDEO, ®s, ®s);
  268. }
  269.  
  270.  
  271.  
  272.  
  273.  
  274. /*
  275.  *    SCREEN_CO - write a character to the screen. this
  276.  *                routine increments the cursor position
  277.  *                after writing. normal C88 puts and printf
  278.  *                statements can also be used to write to the
  279.  *                screen.
  280.  *
  281.  *    Usage:   screen_co (character);
  282.  */
  283.  
  284. int 
  285. screen_co (char ch)
  286. {
  287.     regs.h.al = ch;
  288.     regs.h.bh = Scr_Page;
  289.     regs.h.ah = WRITETTY;
  290.     int86 (VIDEO, ®s, ®s);
  291. }
  292.  
  293.  
  294.  
  295.  
  296. /*
  297.  *    SINP - screen input (read character from the screen).
  298.  *
  299.  *    Usage:     character = sinp ();
  300.  */
  301.  
  302. int 
  303. sinp (void)
  304. {
  305.     regs.h.bh = Scr_Page;
  306.     regs.h.ah = READCH;
  307.     int86 (VIDEO, ®s, ®s);
  308.     return ((int) (regs.h.al) ? regs.h.al : ' ');
  309. }
  310.  
  311.  
  312.  
  313.  
  314.  
  315. /*
  316.  *    CURSOR_OFF - turn cursor off.
  317.  *
  318.  *    Usage:        cursor_off ();
  319.  */
  320.  
  321. int 
  322. cursor_off (void)
  323. {
  324.     if (Scr_Mode < 4 || Scr_Mode == 7)
  325.     {
  326.         regs.x.cx = 0x0f00;
  327.         regs.h.ah = SETCURTYP;
  328.         int86 (VIDEO, ®s, ®s);
  329.     }
  330. }
  331.  
  332.  
  333.  
  334.  
  335. /*
  336.  *    CURSOR_ON - turn cursor back on.
  337.  *
  338.  *    Usage:       cursor_on ();
  339.  */
  340.  
  341. int 
  342. cursor_on (void)
  343. {
  344.     if (Scr_Mode == 7)
  345.         regs.x.cx = 0x0C0D;
  346.     else
  347.     if (Scr_Mode < 4)
  348.         regs.x.cx = 0x0607;
  349.     else
  350.         return;
  351.     regs.h.ah = SETCURTYP;
  352.     int86 (VIDEO, ®s, ®s);
  353. }
  354.  
  355.  
  356.  
  357.  
  358.  
  359. /*
  360.  *    APUTS - write a string in the current attribute to the screen.
  361.  *
  362.  *    Usage:      aputs (attr, "Write this out\n");
  363.  */
  364.  
  365. int 
  366. aputs (int a, char *s)
  367. {
  368.     while (*s)
  369.         aput (a, *s++);
  370. }
  371.  
  372.  
  373.  
  374.  
  375. /*
  376.  *    APUT - write a string and attributes to the screen.
  377.  *               the cursor is moved normally
  378.  *
  379.  *    Usage:     aput (INVERSE, 'x');
  380.  */
  381.  
  382. int 
  383. aput (int attr, char ch)
  384. {
  385.     switch (ch)
  386.     {
  387.         default:
  388.             regs.h.al = ch;
  389.             regs.h.bl = attr;
  390.             regs.x.cx = 1;
  391.             regs.h.bh = Scr_Page;
  392.             regs.h.ah = WRITEACH;
  393.             int86 (VIDEO, ®s, ®s);
  394.             regs.h.ah = GETCURSOR;
  395.             int86 (VIDEO, ®s, ®s);
  396.             if (!(++regs.h.dl < Scr_Cols))
  397.             {
  398.                 regs.h.dl = 0;
  399.                 if (!(++regs.h.dh < Scr_Rows))
  400.                 {
  401.                     scroll_up (1, 0, 0, 23, 79);
  402.                     regs.h.dl = 0;
  403.                     regs.h.dh = 23;
  404.                 }
  405.             }
  406.             regs.h.bh = Scr_Page;
  407.             regs.h.ah = SETCURSOR;
  408.             int86 (VIDEO, ®s, ®s);
  409.             break;
  410.         case 10:
  411.             regs.h.ah = GETCURSOR;
  412.             int86 (VIDEO, ®s, ®s);
  413.             regs.h.dl = 0;
  414.             if (!(++regs.h.dh < Scr_Rows - 2))
  415.             {
  416.                 scroll_up (0, 0, 0, Scr_Rows - 2, 79);
  417.                 regs.h.dl = 0;
  418.                 regs.h.dh = Scr_Rows - 2;
  419.             }
  420.             regs.h.bh = Scr_Page;
  421.             regs.h.ah = SETCURSOR;
  422.             int86 (VIDEO, ®s, ®s);
  423.             break;
  424.         case 13:
  425.             regs.h.ah = GETCURSOR;
  426.             int86 (VIDEO, ®s, ®s);
  427.             regs.h.dl = 0;
  428.             regs.h.bh = Scr_Page;
  429.             regs.h.ah = SETCURSOR;
  430.             int86 (VIDEO, ®s, ®s);
  431.             break;
  432.     }
  433. }
  434.